package vip.xiaonuo.modular.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.modular.entity.Yuanxi;
import vip.xiaonuo.modular.enums.YuanxiExceptionEnum;
import vip.xiaonuo.modular.mapper.YuanxiMapper;
import vip.xiaonuo.modular.param.YuanxiParam;
import vip.xiaonuo.modular.service.YuanxiService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 院系管理service接口实现类
 *
 * @author 春非昔日春
 * @date 2023-03-08 16:34:19
 */
@Service
public class YuanxiServiceImpl extends ServiceImpl<YuanxiMapper, Yuanxi> implements YuanxiService {

    @Override
    public PageResult<Yuanxi> page(YuanxiParam yuanxiParam) {
        QueryWrapper<Yuanxi> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(yuanxiParam)) {

            // 根据院系名称 查询
            if (ObjectUtil.isNotEmpty(yuanxiParam.getName())) {
                queryWrapper.lambda().like(Yuanxi::getName, yuanxiParam.getName());
            }
            // 根据院系简介 查询
            if (ObjectUtil.isNotEmpty(yuanxiParam.getIntroduction())) {
                queryWrapper.lambda().like(Yuanxi::getIntroduction, yuanxiParam.getIntroduction());
            }
            // 根据院长 查询
            if (ObjectUtil.isNotEmpty(yuanxiParam.getPresident())) {
                queryWrapper.lambda().like(Yuanxi::getPresident, yuanxiParam.getPresident());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Yuanxi> list(YuanxiParam yuanxiParam) {
        return this.list();
    }

    @Override
    public void add(YuanxiParam yuanxiParam) {
        Yuanxi yuanxi = new Yuanxi();
        BeanUtil.copyProperties(yuanxiParam, yuanxi);
        this.save(yuanxi);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<YuanxiParam> yuanxiParamList) {
        yuanxiParamList.forEach(yuanxiParam -> {
        Yuanxi yuanxi = this.queryYuanxi(yuanxiParam);
            this.removeById(yuanxi.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(YuanxiParam yuanxiParam) {
        Yuanxi yuanxi = this.queryYuanxi(yuanxiParam);
        BeanUtil.copyProperties(yuanxiParam, yuanxi);
        this.updateById(yuanxi);
    }

    @Override
    public Yuanxi detail(YuanxiParam yuanxiParam) {
        return this.queryYuanxi(yuanxiParam);
    }

    /**
     * 获取院系管理
     *
     * @author 春非昔日春
     * @date 2023-03-08 16:34:19
     */
    private Yuanxi queryYuanxi(YuanxiParam yuanxiParam) {
        Yuanxi yuanxi = this.getById(yuanxiParam.getId());
        if (ObjectUtil.isNull(yuanxi)) {
            throw new ServiceException(YuanxiExceptionEnum.NOT_EXIST);
        }
        return yuanxi;
    }
}
